package com.person.dic.action;

import java.util.List;
import java.util.regex.Pattern;

import com.person.commons.struts2.BaseAction;
import com.person.dic.model.DicPara;
import com.person.dic.service.DicParaService;

public class DicParaAction extends BaseAction {
	private DicParaService dicParaService;
	private DicPara para;
	private List<DicPara> paras;
	private String paraId;
	private String paraNo;
	private String paraName;
	private String paraNameEn;
	private String paraValue;
	private String paraOrder;
	
	
	/**
	 * 查找竞赛参数列表
	 * @return  角色列表
	 */
	public String index()
	{
		paras=dicParaService.seachAll();
		return "search";
	}
	
	
	/**
	 * 返回其他字典管理首页
	 * @return  角色列表
	 */
	public String goCreate()
	{
		return "create";
	}
	
	/**
	 * 新增角色
	 * @param para 对象
	 * @return info 是否成功
	 */
	
	public String create(){
		char s;
		if(para.getParaNo()==null&&"".equals(para.getParaNo())){
			info="编号不可为空";
			return "info";
		}
		
		if(para.getParaValue()==null&&"".equals(para.getParaValue())){
			info="参数值不可为空";
			return "info";
		}

		
		 //判断paraId是否为空，为空代表是新增，否则为修改
		   if(para.getParaId()==null||"".equals(para.getParaId()))
		   {
			s=dicParaService.create(para);
		   }
		   else{
		    s=dicParaService.update(para);
		   }
		   if('1'==s)
			      return "research";
			   else if('0'==s)
			   {
				info="操作失败";
				 return "info"; 
			   }
			   else
			   {
					info="代码重复";
					 return "info"; 
				   }
	}
	

	/**
	 * 查找角色
	 * @param para 对象
	 * @return info 是否成功
	 */
	
	public String goUpdate(){
		para=dicParaService.searchById(paraId);
		 return "create";
	}
	
	/**
	 * 修改角色
	 * @param season 对象
	 * @return info 是否成功
	 */
	
	public String update(){
		char s=dicParaService.update(para);
		if(s=='1'){
			info=getText("para.success");
	    	  }
	    	 else if(s=='2')
	    	 {
	    		 info=getText("para.repeat");
	    	 }
	    	 else
	    	 {
	    		 info=getText("para.fail");
	    	 }
		 return "info";
	}

	/**
	 * 删除角色
	 * @param paraId 
	 * @return info 是否成功
	 */
	
	public String delete(){
		char s=dicParaService.delete(paraId);
		if(s=='1'){
			info="操作成功";
	    	  return "research";
	    	  }
	    	 else
	    	 {
	    		 info="操作失败";
	    		 return "info";
	    	 }
	}

	
	 //参数添加数据校验
    public void validateUpdate(){
  		if(para.getParaNo()==null||"".equals(para.getParaNo())){
  			addFieldError("para.paraNo",getText("error.paraNo"));
  		}
  		if(para.getParaValue()==null||"".equals(para.getParaValue())){
  			addFieldError("para.paraValue",getText("error.paraValue"));
  		}
  		else
  		{
  			if(!Pattern.matches("^\\d+$", para.getParaValue().toString())){
 			      addFieldError("para.paraValue",getText("error.paradig"));
	 			}
  		}
    }
  		
  		 //参数修改数据校验
  	    public void validateCreate(){
  	  		if(para.getParaNo()==null||"".equals(para.getParaNo())){
  	  			addFieldError("para.paraNo",getText("error.paraNo"));
  	  		}
  	  		if(para.getParaValue()==null||"".equals(para.getParaValue())){
  	  			addFieldError("para.paraValue",getText("error.paraValue"));
  	  		}
  	  		else
  	  		{
  	  			if(!Pattern.matches("^\\d+$", para.getParaValue().toString()))
  	 			      addFieldError("para.paraValue",getText("error.paradig"));
  	  		}
  	    }
    

	public DicParaService getDicParaService() {
		return dicParaService;
	}


	public void setDicParaService(DicParaService dicParaService) {
		this.dicParaService = dicParaService;
	}


	public DicPara getPara() {
		return para;
	}


	public void setPara(DicPara para) {
		this.para = para;
	}


	public List<DicPara> getParas() {
		return paras;
	}


	public void setParas(List<DicPara> paras) {
		this.paras = paras;
	}


	public String getParaId() {
		return paraId;
	}


	public void setParaId(String paraId) {
		this.paraId = paraId;
	}


	public String getParaNo() {
		return paraNo;
	}


	public void setParaNo(String paraNo) {
		this.paraNo = paraNo;
	}


	public String getParaName() {
		return paraName;
	}


	public void setParaName(String paraName) {
		this.paraName = paraName;
	}


	public String getParaNameEn() {
		return paraNameEn;
	}


	public void setParaNameEn(String paraNameEn) {
		this.paraNameEn = paraNameEn;
	}


	public String getParaValue() {
		return paraValue;
	}


	public void setParaValue(String paraValue) {
		this.paraValue = paraValue;
	}


	public String getParaOrder() {
		return paraOrder;
	}


	public void setParaOrder(String paraOrder) {
		this.paraOrder = paraOrder;
	}

	}
